package windows;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;

import bussinesmodel.CAdministrator;

import utilities.CListModel;
import dataTables.CDataTableIngredients;
import dataTables.CDataTableRestaurants;
import dbblink.CQuery;


public class CAddSalePane extends JPanel 
{
	
	    private javax.swing.JButton buttonAdd;
	    private javax.swing.JButton buttonSave;
	    private javax.swing.JButton buttonSearchSale;
	    private javax.swing.JComboBox comboDay;
	    private javax.swing.JComboBox comboMonth;
	    private javax.swing.JComboBox comboYear;
	    private javax.swing.JTextField fieldCost;
	    private javax.swing.JLabel labelCost;
	    private javax.swing.JLabel labelDate;
	    private javax.swing.JLabel labelRestaurant;
	    private javax.swing.JLabel labelTittle;
	    private javax.swing.JList listADDEDRestaurant;
	    private javax.swing.JList listADDRestaurant;
	    private javax.swing.JScrollPane scrollListADDEDRestaurant;
	    private javax.swing.JScrollPane scrollListADDRestaurant;
	    private javax.swing.JSeparator separadorTitulo;
	    private CQuery query;
	    private CAdministrator administrator;
	    private Object[] a;
	    private String[] saListADD;
	    private String[] asDay=new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }; 
	    private String[] asMonth=new String[] { "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre" };
	    private String[] asYear=new String[] { "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020" };
	    String sDate;
	    
	    int nDay,nMonth,nYear;
	    
	    public CAddSalePane() 
	    {
	        initComponents();
	    }
	    
	    
	    private void initComponents() 
	    {

	        labelTittle = new javax.swing.JLabel();
	        separadorTitulo = new javax.swing.JSeparator();
	        comboDay = new javax.swing.JComboBox();
	        comboMonth = new javax.swing.JComboBox();
	        comboYear = new javax.swing.JComboBox();
	        labelDate = new javax.swing.JLabel();
	        labelRestaurant = new javax.swing.JLabel();
	        scrollListADDRestaurant = new javax.swing.JScrollPane();
	        listADDRestaurant = new javax.swing.JList();
	        labelCost = new javax.swing.JLabel();
	        fieldCost = new javax.swing.JTextField();
	        buttonSearchSale = new javax.swing.JButton();
	        buttonSave = new javax.swing.JButton();
	        
	        buttonAdd = new javax.swing.JButton();
	      
	        scrollListADDEDRestaurant = new javax.swing.JScrollPane();
	        listADDEDRestaurant = new javax.swing.JList();

	        
	        setLayout(null);

	        labelTittle.setFont(new java.awt.Font("Tahoma", 1, 14)); 
	        labelTittle.setForeground(new java.awt.Color(0, 0, 102));
	        labelTittle.setText("Ingrese la Venta");
	        add(labelTittle);
	        labelTittle.setBounds(50, 30, 210, 17);

	        separadorTitulo.setForeground(new java.awt.Color(0, 0, 0));
	        add(separadorTitulo);
	        separadorTitulo.setBounds(40, 52, 220, 2);

	        comboDay.setModel(new javax.swing.DefaultComboBoxModel(asDay));
	        add(comboDay);
	        comboDay.setBounds(90, 70, 50, 20);

	        comboMonth.setModel(new javax.swing.DefaultComboBoxModel(asMonth));
	        add(comboMonth);
	        comboMonth.setBounds(150, 70, 90, 20);

	        comboYear.setModel(new javax.swing.DefaultComboBoxModel(asYear));
	        add(comboYear);
	        comboYear.setBounds(260, 70, 70, 20);

	        labelDate.setText("Fecha:");
	        add(labelDate);
	        labelDate.setBounds(50, 70, 50, 14);

	        labelRestaurant.setText("Restaurante:");
	        add(labelRestaurant);
	        labelRestaurant.setBounds(50, 110, 80, 14);

	        
	        listADDRestaurant.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	        
	 
	        scrollListADDRestaurant.setViewportView(listADDRestaurant);

	        add(scrollListADDRestaurant);
	        scrollListADDRestaurant.setBounds(150, 110, 100, 70);

	        labelCost.setText("Valor:");
	        add(labelCost);
	        labelCost.setBounds(50, 200, 50, 14);

	        fieldCost.addKeyListener(new KeyAdapter() {
				public void keyTyped(KeyEvent e) {
					char caracter = e.getKeyChar();
					// Verificar si la tecla pulsada no es un digito
					if (((caracter < '0') || (caracter > '9'))
							&& (caracter != KeyEvent.VK_BACK_SPACE)) {
						e.consume(); // ignorar el evento de teclado
					}
				}
			});
	        add(fieldCost);
	        fieldCost.setBounds(90, 200, 180, 20);

	        buttonSearchSale.setText("Buscar Venta");
	        buttonSearchSale.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	               
	            	administrator.showSaleSearchPane();
	            }
	        });
	        add(buttonSearchSale);
	        buttonSearchSale.setBounds(90, 240, 140, 23);

	        buttonSave.setText("Guardar");
	        buttonSave.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) 
	            {
	            	try
	            	{
	            		

	    				nDay=comboDay.getSelectedIndex();	    			
	    				nMonth=comboMonth.getSelectedIndex();
	    				nYear=comboYear.getSelectedIndex();
	            		sDate =asYear[nYear]+"-"+(nMonth+1)+"-"+asDay[nDay];
		            	System.out.println(""+sDate);
		            	administrator.saveSale(sDate, fieldCost.getText(), a);
		            	
	            	}
	            	catch(Exception g)
	            	{
	            		JOptionPane.showMessageDialog(null, "Agregue el Restaurante","Error",JOptionPane.ERROR_MESSAGE);
	            	}
	            	
	            }
	        });
	        
	       add(buttonSave);
	        buttonSave.setBounds(240, 240, 71, 23);

	       

	        buttonAdd.setText(">>>");
	        buttonAdd.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	            	a = listADDRestaurant.getSelectedValues();	
		            listADDEDRestaurant.setListData(listADDRestaurant.getSelectedValues());
	            }
	        });
	        add(buttonAdd);
	        buttonAdd.setBounds(260, 120, 60, 23);

	     

	       
	        scrollListADDEDRestaurant.setViewportView(listADDEDRestaurant);

	        add(scrollListADDEDRestaurant);
	        scrollListADDEDRestaurant.setBounds(330, 110, 100, 70);

	        fillList();
	        setLocation(230, 60);
			setSize(400, 500);
	    }

	    
	   
	    	
	    	
	 	    	
	    
	    
	    public void fillList()
		{
			
		    query = new CQuery();
			String sQuery="Select * From restaurant";
			ArrayList<CDataTableRestaurants> aRestaurants;
			aRestaurants=query.showRestaurants(sQuery);
			saListADD = new String[aRestaurants.size()];
			
			CDataTableRestaurants aux;
			
			
			for(int i=0;i<saListADD.length;i++)
			{
				aux=aRestaurants.get(i);
				//System.out.println(aux.getSIngredientName());
				saListADD[i]=aux.getSNameRestaurant();
			}
		    
			listADDRestaurant.setModel(new CListModel(saListADD)); 
			

		}
	    
	    
		public void cleanFields()
		{
			fieldCost.setText("");
			String sanewListADD[]=new String[1];
			listADDEDRestaurant.setModel(new CListModel(sanewListADD)); 
			
		}

		public void setAdministrator(CAdministrator administrator) {
			this.administrator = administrator;
		}


		
			
		    	
			
		
		
		
		    
		    
	    	
	    	
	    	
	    	
			
		
	

}
